import 'package:bike_ride/common/mvvm/base_page.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/common/widget/dialog/mydialog.dart';
import 'package:bike_ride/common/widget/page_title_widget.dart';
import 'package:bike_ride/common/widget/super_button_widget.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:bike_ride/model/unlock/view_model/unlock_vm.dart';
import 'package:bike_ride/res/style/color_styles.dart';
import 'package:bike_ride/res/style/text_styles.dart';
import 'package:bike_ride/router/navigator_util.dart';
import 'package:bike_ride/router/routers.dart';
import 'package:flutter/material.dart';

///确认开锁
class UnLockView extends BaseStatefulPage<UnLockViewModel> {
  final String vid;

  const UnLockView({required this.vid, super.key});

  @override
  UnLockViewState createState() => UnLockViewState();
}

class UnLockViewState
    extends BaseStatefulPageState<UnLockView, UnLockViewModel> {

  bool get needRecharge {
    final serverAreaInfoByBike = AccountManager.areaInfoM;
    final financeInfo = viewModel!.discountStatM;
    return serverAreaInfoByBike?.isRechargeBalance == 1 &&
        serverAreaInfoByBike!.minBalance > 0 &&
        financeInfo != null &&
        (financeInfo.balanceAmount) + (financeInfo.giftBalanceAmount) < serverAreaInfoByBike.minBalance;
  }

  @override
  void initAttribute() {}

  @override
  void initObserver() {}

  @override
  UnLockViewModel viewBindingViewModel() {
    return UnLockViewModel()..viewState = this;
  }

  void notifyChange() {
    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget appBuild(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      backgroundColor: ColorStyles.color_F3F4F5,
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            PageTitleWidget(title: S.current.confirmUnlock),

            SizedBox(
              height: 480.rpx,
              width: MediaQuery.of(context).size.width,
              child: Stack(
                children: [
                  Image.asset(
                    'assets/common_v2/helmet_banner.png',
                    fit: BoxFit.fitWidth,
                  ),
                  Positioned(
                    top: 18.rpx,
                    left: 0,
                    right: 0,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Text(
                          S.current.rideWithHelmet,
                          style: TextStyles.style_big_tips,
                        ),
                        SizedBox(
                          height: 10.rpx,
                        ),
                        Text(
                          S.current.returnHelmetRules,
                          style: TextStyles.style_small_tips,
                        ),
                      ],
                    ),
                  ),
                  Positioned(
                    top: 126.rpx,
                    right: 0,
                    child: GestureDetector(
                      onTap: () {
                        NavigatorUtil.push(context, Routers.freightView);
                      },
                      child: Container(
                        padding: EdgeInsets.only(
                            left: 16.rpx,
                            top: 8.rpx,
                            bottom: 8.rpx,
                            right: 8.rpx),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(16.rpx),
                                bottomLeft: Radius.circular(16.rpx)),
                            color: Colors.white70),
                        child: Text(
                          S.current.chargeStandard,
                          style: TextStyle(
                              color: ColorStyles.color_999999,
                              fontSize: 22.rpx),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildItem(viewModel!.bikeInfoM?.bikeUseStatus?.isSocFix == 1? S.current.goodPower:S.current.badPower, '${viewModel!.bikeInfoM?.bmsInfo?.soc??'0'}', '%'),
                _buildItem(S.current.expectedRide, '${viewModel!.bikeInfoM?.bikeUseStatus?.riddingMile??'0'}', 'km'),
                _buildItem('${S.current.startPrice}(${viewModel!.bikeInfoM?.feeInfo?.startMin??'0'}${S.current.mins})', '${viewModel!.bikeInfoM?.feeInfo?.startPrice??'0'}', S.current.yuan, showTips: true),
              ],
            ),

            ///骑车优惠

            ///温馨提示
            Padding(
              padding: EdgeInsets.only(left: 28.rpx),
              child: Text(
                S.current.unlockVehicleTips,
                style: TextStyles.style_small_tips,
                textAlign: TextAlign.start,
              ),
            )
          ],
        ),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.symmetric(horizontal: 28.rpx,vertical: 22.rpx),
        decoration: BoxDecoration(
          color: ColorStyles.color_FFFFFF,
          borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8.rpx),
              topRight: Radius.circular(8.rpx)),
        ),
        child: Row(mainAxisSize: MainAxisSize.min, children: [
          Expanded(
            child: SuperButtonWidget(
              btnText: viewModel!.isError?S.current.tryAgain:S.current.confirmUnlock,
              onPressed: () {
                if(viewModel!.isError){
                  viewModel!.getBikeInfo(widget.vid);
                }else{
                  if(needRecharge){
                    viewModel?.getFinanceInfo().then((_){
                      if(needRecharge && (mounted && context.mounted)){
                        showAlertDialog(context: context,title: '', content: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Image.asset('assets/applet/ic_fail.png',width: 100.rpx,height: 100.rpx,fit: BoxFit.cover,),
                            SizedBox(height: 12.rpx,),
                            Text('${S.current.walletNotEnough}${AccountManager.areaInfoM?.minBalance}${S.current.yuan}，${S.current.walletNotEnough2}!',style: TextStyle(fontSize: 28.rpx,color: Colors.black87),)
                          ],
                        ),confirmName: S.current.toRecharge,onConfirm: (){
                          Navigator.pop(context);
                          NavigatorUtil.push(context, Routers.walletView,arguments: {
                            'show_charge_dialog': true,
                          }).then((value) {
                            viewModel?.getFinanceInfo();
                            viewModel?.getBrandAreaInfo();
                          });
                        });
                      }
                    });
                  }else{
                    NavigatorUtil.push(context, Routers.cyclingView,replace: true,arguments: {
                      'vid': viewModel!.bikeInfoM?.bikeInfo?.id??widget.vid,
                    });
                  }
                }
              },
            ),
          ),
        ]),
      ),
    );
  }

  Widget _buildItem(String title, String value, String unit,
      {bool showTips = false}) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          title,
          style: TextStyle(color: ColorStyles.color_666666, fontSize: 26.rpx),
        ),
        SizedBox(
          height: 10.rpx,
        ),
        Row(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            Text(
              value,
              style:
                  TextStyle(color: ColorStyles.color_333333, fontSize: 48.rpx),
            ),
            Text(
              unit,
              style:
                  TextStyle(color: ColorStyles.color_999999, fontSize: 24.rpx),
            ),
            if (showTips)
              GestureDetector(
                onTap: () {
                  NavigatorUtil.push(context, Routers.freightView);
                },
                child: Icon(
                  Icons.help_outline_rounded,
                  color: ColorStyles.color_999999,
                  size: 24.rpx,
                ),
              )
          ],
        ),
      ],
    );
  }
}
